<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="4">
          <el-button type="primary">添加用户角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList" border stripe style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item, index) of scope.row.children"
              :key="item.id"
              :class="[
                'el-bbottom',
                index === 0 ? 'el-btop' : '',
                'row-center',
              ]"
            >
              <!-- 一级权限渲染 -->
              <el-col :span="5">
                <el-tag
                  type="primary"
                  closable
                  @close="deleteUserRights(scope.row, item.id)"
                  >{{ item.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级/三级权限渲染区域 -->
              <el-col :span="19">
                <!-- 二级权限渲染 -->
                <el-row
                  v-for="(item2, index2) of item.children"
                  :class="[index2 === 0 ? '' : 'el-btop', 'row-center']"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="deleteUserRights(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限渲染区域 -->
                  <el-col :span="18" class="item3">
                    <el-tag
                      closable
                      type="warning"
                      @close="deleteUserRights(scope.row, item3.id)"
                      v-for="(item3) of item2.children"
                      :key="item3.id"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column 
         label="姓名"
         width="100"
         prop="roleName"> </el-table-column>
        <el-table-column prop="roleDesc" label="职位"
        
        > </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteUserRights"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showSetRightsDialog(scope.row)"
              >权限设置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="setRightsDialogVisible"
      width="50%"
      @close="resetLeafKeys"
    >
      <el-tree      
      :data="rolesTree" 
      :props="treeProps"
      show-checkbox
      node-key="id"
      default-expand-all
      :default-checked-keys="leafKyes"
      ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //所有职位列表数据
      rolesList: [],
      //职位设置dialog框的显示/隐藏
      setRightsDialogVisible:false,
      //所有职位树形数据
      rolesTree:[],
      treeProps:{
        children:'children',
        label:'authName'
      },
      //默认选中节点的id值的数组
      leafKyes:[],
      //设置权限的职位的id
      rolesId:''
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    //获取角色列表
    async getRoles() {
      let {
        data: { data, meta },
      } = await this.$api.getRolesData()
      if (meta.status === 200) {
        this.rolesList = data
      } else {
        this.$message.error('获取角色列表数据失败')
      }
    },
    //删除职位权限
    async deleteUserRights(roles, rightsId) {
      let res = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch(() => this.$message.info('已取消删除'))
      if (res === 'confirm') {
        let {
          data: { data, meta },
        } = await this.$api.deleteRolesRights(roles.id, rightsId)
        if (meta.status === 200) {
          roles.children = data
          this.$message.success('删除成功')
          //this.getUsersList()
        } else {
          this.$message.error('删除失败')
        }
      }
    },
    //设置职位权限
    async showSetRightsDialog(roles) {
      this.rolesId = roles.id
      let {data:{data,meta}} = await this.$api.getRightsTree()
      if (meta.status === 200) {
        this.rolesTree = data
      }
      this.getLeafKeys(roles,this.leafKyes)
      this.setRightsDialogVisible = true
    },
    //获取已有权限的节点的id(只要三级权限的节点,三级权限没有children)
    getLeafKeys(nodes,arr) {
      if (nodes.children) {
        nodes.children.forEach(item => {
        this.getLeafKeys(item,arr)
      });
      }else {
         arr.push(nodes.id)
      }
    },
    //每次关闭rightsDialog弹框时,将leafKyes重置为空(避免id重复)
    resetLeafKeys() {
      this.leafKyes = []
    },
    //给职位设置权限
    async setRights() {
      let treeRef = this.$refs.treeRef
      let keys = [
        ...treeRef.getCheckedKeys(),
        ...treeRef.getHalfCheckedKeys()
        ].join(',')
      let {data:{_,meta}} = await this.$api.allotRights(this.rolesId,{rids:keys})
      if (meta.status === 200) {
        this.$message.success('权限更新成功')
        this.getRoles()
        this.setRightsDialogVisible = false
      }else {
        this.$message.error('权限更新失败')
      }
    }
  },
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  height: 20px;
  line-height: 20px;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #ccc;
}
.el-table {
  margin-top: 10px;
  width: 100%;
  .el-row {
    margin: 7px;
    padding: 7px;
  }
  .row-center {
    display: flex;
    align-items: center;
  }
  .el-btop {
    border-top: 1px solid #ccc;
  }
  .el-bbottom {
    border-bottom: 1px solid #ccc;
  }
  .item3 {
    .el-tag {
      margin: 5px;
    }
  }
}
</style>
